//
//  GroupView.m
//  QQ_Youngsher
//
//  Created by Maxwell on 16/1/15.
//  Copyright © 2016年 LLLTeam. All rights reserved.
//

#define SCREEN_W [[UIScreen mainScreen] bounds].size.width
#define SCREEN_H [[UIScreen mainScreen] bounds].size.height

#import "GroupView.h"

@implementation GroupView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        _switchImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, (frame.size.height - 15) / 2, 15, 15)];
        [self addSubview:_switchImageView];
        
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(35, 0, SCREEN_W - 100, frame.size.height)];
        [self addSubview:_titleLabel];
        
        _numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_W - 55, 0, 50, frame.size.height)];
        _numberLabel.textAlignment = NSTextAlignmentRight;
        _numberLabel.font = [UIFont systemFontOfSize:13];
        _numberLabel.textColor = [UIColor lightGrayColor];
        [self addSubview:_numberLabel];
        
        _isOpen = NO;
        
        UIButton *groupBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        groupBtn.frame = frame;
        [groupBtn addTarget:self action:@selector(clickGroupInSection:open:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:groupBtn];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 1)];
        [self addSubview:lineView];
        
        self.backgroundColor = [UIColor colorWithRed:0.9373 green:0.9373 blue:0.9373 alpha:1.0];
    }
    return self;
}

#pragma GroupViewDelegate
- (void)clickGroupInSection:(NSInteger)section open:(BOOL)isopen{
    _isOpen = !_isOpen;
    [self.delegate openOrCloseGroupInSection:self.section];
    
    if (_isOpen) {
        [UIView animateWithDuration:0.3 delay:0 options:(UIViewAnimationOptionCurveLinear) animations:^{
            _switchImageView.transform = CGAffineTransformMakeRotation(M_PI_2);
        } completion:^(BOOL finished) {
            
        }];
    }else{
        [UIView animateWithDuration:0.3 delay:0 options:(UIViewAnimationOptionCurveLinear) animations:^{
            _switchImageView.transform = CGAffineTransformMakeRotation(0);
        } completion:^(BOOL finished) {
            
        }];
    }
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
